import { createRouter, createWebHistory } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { tokenStore } from "@/stores/tokenStore";
import pages from "./pages";
import { UploadType } from "@/_gx/TsEnums";

//构建路由对象
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: pages,
});

//在任何导航前执行
router.beforeEach((to, from, next) => {
    //进度条开始
    NProgress.start();

    // console.log(`router-from:${Gx.SerializeObject(from)}`);
    // console.log(`router-to:${Gx.SerializeObject(to)}`);

    //获取store中的token和qiniu，如果没有则跳转到登录
    let store = tokenStore();
    let token = store.token;
    let qiniu = store.qiniu;
    if (to.name != "login" && to.name != "suplogin" && (Gx.IsEmpty(token) || (Gx.IsEmpty(qiniu) && Config._UPLOAD_TYPE == UploadType.Qiniu))) {
        console.log("路由守卫生效，跳转到登录！");
        return next({ path: "/login" });
    }

    if (to.matched.length === 0) {
        next({ path: "/error" });
    } else {
        next();
    }
});

//在每次导航后执行
router.afterEach((to, from) => {
    //进度条结束
    NProgress.done();
});

export default router;
